package com.example.parttimejob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.ReviewsMapper;
import com.example.parttimejob.mapper.UserMapper;
import com.example.parttimejob.pojo.entity.Reviews;
import com.example.parttimejob.pojo.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
class ReviewInfo{
    private String username;
    private String comments;
    private Date timestamps;
    private String img;
}
@RestController
@CrossOrigin
public class Reviewscontroller {
    @Autowired
    ReviewsMapper reviewsmapper;
    @Autowired
    UserMapper usermapper;

    @GetMapping("/reviews/reviewer/{reviewer_id}")
    public List<Reviews> getReviewsFromReviewer(@PathVariable int reviewer_id) {
        QueryWrapper<Reviews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reviewer_id", reviewer_id);
        return reviewsmapper.selectList(queryWrapper);
    }

    @GetMapping("/reviews/target/{target_id}")
    public List<ReviewInfo> getReviewsFromTarget(@PathVariable int target_id) {
        QueryWrapper<Reviews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("target_id", target_id).eq("reviewer_category", "用户");
        List<Reviews> reviewsList = reviewsmapper.selectList(queryWrapper);

        List<ReviewInfo> reviewInfoList = new ArrayList<>();

        for (Reviews review : reviewsList) {
            // 查询对应的用户信息
            User user = usermapper.selectById(review.getReviewerId());

            // 构造 ReviewInfo 对象
            ReviewInfo reviewInfo = new ReviewInfo();
            reviewInfo.setUsername(user.getUsername());
            reviewInfo.setComments(review.getComments());
            reviewInfo.setTimestamps(review.getTimestamps());
            reviewInfo.setImg(user.getImg());

            // 将结果添加到列表中
            reviewInfoList.add(reviewInfo);
        }

        return reviewInfoList;
    }


    @DeleteMapping("/reviews/{reviewId}")
    public ResponseEntity<Void> deleteReview(@PathVariable("reviewId") Integer id) {
        int result = reviewsmapper.deleteById(id);
        return (result > 0) ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PutMapping("/reviews/{reviewId}")
    public ResponseEntity<Reviews> updateReview(@PathVariable int reviewId, @RequestBody Reviews review) {
        Reviews existingReview = reviewsmapper.selectById(reviewId);
        if (existingReview != null) {
            review.setReviewId(reviewId); // 设置要更新的评论ID
            reviewsmapper.updateById(review);
            return ResponseEntity.ok(review);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @PostMapping("/reviews")
    public Reviews createreviews(@RequestBody Reviews user) {
        user.setReviewerCategory("用户");
        user.setTimestamps(new Date());
        reviewsmapper.insert(user);
        return user;
    }
}

